package onlinejudge.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import onlinejudge.vo.UserVo;
import onlinejudge.entity.User;
import onlinejudge.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	
	/**
	 * 登录
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request) {
		UserVo currentUser=userService.login(user);
		HttpSession session=request.getSession();
		session.setAttribute("currentUser", currentUser);
		if(currentUser==null) {
			return "redirect:/problem/list?LoginError=error&pager.offset=0";
		}
		if(currentUser.getRoleName().equals("admin")) {
			return "redirect:/admin/index";
		}
		return "redirect:/problem/list?pager.offset=0";
	}
	/**
	 * 退出登录
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("currentUser");
		return "redirect:/problem/list?pager.offset=0";
	}
	/**
	 * 注册接口
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/register")
	public String register(User user,HttpServletRequest request,HttpServletResponse response) {
		User registerUser=userService.register(user);
		if(registerUser==null) {
			return "redirect:/problem/list?RegisterError=error&pager.offset=0";
		}
		return "redirect:/problem/list?RegisterSuccess=success&pager.offset=0";
	}
	/**
	 * 返回所有用户接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public String list(int page, int limit) {
//		{"msg":"success","code":"0","data":[{}]}
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("page", (page-1)*limit);
		parameters.put("limit", limit);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("count", userService.getUserCount());
		json.put("data",userService.queryAllUser(parameters));
		return json.toJSONString();
	}
	/**
	 * 用户删除接口
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestBody Map<String,Object> map) {
		Integer userId=(Integer) map.get("userId");
		userService.deleteUser(userId);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data", "");
		return json.toJSONString();
	}
	/**
	 * 用户修改接口
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public String update(@RequestBody Map<String,Object> map) {
		String userName=(String) map.get("userName");
		Integer userId=(Integer) map.get("userId");
		String passWord=(String) map.get("passWord");
		String email=(String) map.get("email");
		String userImg=(String) map.get("userImg");
		UserVo user=new UserVo(userId, userName, passWord, email, userImg, "client");
		userService.updateUser(user);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data", "");
		return json.toJSONString();
	}
	/**
	 * 修改个人信息页面导向
	 * @return
	 */
	@RequestMapping("/form")
	public String form() {
		return "client/userForm";
	}
	/**
	 * 个人空间页面导向
	 * @return
	 */
	@RequestMapping("/myspace")
	public String myspace(HttpServletRequest request) {
		UserVo currentUser=(UserVo) request.getSession().getAttribute("currentUser");
		Integer userId=currentUser.getUserId();
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("userId", userId);
		return "client/userSpace";
	}
	/**
	 * 用户修改个人信息
	 * @return
	 */
	@RequestMapping("/saveInfo")
	public String saveInfo(UserVo user,HttpServletRequest request) {
		userService.updateUser(user);
		//更新session
		UserVo currentUser=userService.getUser(user.getUserId());
		request.getSession().setAttribute("currentUser", currentUser);
		return "redirect:/user/myspace";
	}
	/**
	 * 访问其他人空间页面导向
	 * @return
	 */
	@RequestMapping("/otherspace")
	public String otherspace(Integer userId,HttpServletRequest request) {
		UserVo otherUser=userService.getUser(userId);
		request.setAttribute("otherUser",otherUser );
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("userId",userId);
		return "client/userSpace";
	}


	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		return "client/index";
	}
}
